package com.niit.controller;

import com.niit.entity.Notification;
import com.niit.entity.User;
import com.niit.service.NotificationService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/notification")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    @GetMapping("/view/{id}")
    public Notification getNotificationView(@PathVariable Integer id) {
        return notificationService.getNotificationById(id);
    }

    @GetMapping("/findAll")
    public Page<Notification> findNotifications(
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Byte level,
            @RequestParam(required = false) Byte type,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(defaultValue = "1") Integer loginType) {
	    return notificationService.getNotifications(title, level, type, PageRequest.of(page - 1, size), loginType);
    }

    @PostMapping
    public void createNotification(@RequestBody Notification notification) {
        notificationService.createNotification(notification);
    }

    @PutMapping("/{id}")
    public void updateNotification(
            @PathVariable Integer id,
            @RequestBody Notification notification) {
        notificationService.updateNotification(id, notification);
    }

    @DeleteMapping("/{id}")
    public void deleteNotification(@PathVariable Integer id) {
        notificationService.deleteNotification(id);
    }

    @DeleteMapping("/batch")
    public void deleteNotifications(@RequestBody List<Integer> ids) {
        notificationService.deleteNotifications(ids);
    }

    @PostMapping("/read/{notificationId}")
    public void markAsRead(@PathVariable Integer notificationId,@RequestParam Integer userId) {
        notificationService.markAsRead(userId, notificationId);
    }
}
